<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :model="config"
      :rules="rules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model.trim="config.phone"
          placeholder="请输入接收短信的手机号码"
          class="w"
        />
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >接收短信的手机号码</span
        >
      </el-form-item>
      <el-form-item label="是否开启">
        <el-switch v-model="config.open" active-value="1" inactive-value="0">
        </el-switch>
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >是否开启</span
        >
      </el-form-item>
      <div class="box">
        <el-button type="primary" @click="addChannel()" size="small"
          >添加渠道
        </el-button>
      </div>

      <template>
        <div v-for="(v, i) in config.channels" :key="i" class="box">
          <el-form-item
            label="渠道"
            :prop="'channels.' + i + '.channel'"
            :rules="[{ required: true, message: '必填', trigger: 'blur' }]"
          >
            <el-input
              v-model="config.channels[i].channel"
              placeholder="请输入渠道名称"
              class="w"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="uv最大值"
            :prop="'channels.' + i + '.uv_max'"
            :rules="[{ required: true, message: '必填', trigger: 'blur' }]"
          >
            <el-input
              v-model.number="config.channels[i].uv_max"
              placeholder=""
              class="w"
            >
              <template slot="append">%</template>
            </el-input>
          </el-form-item>

          <el-form-item
            label="uv最小值"
            :prop="'channels.' + i + '.uv_min'"
            :rules="[{ required: true, message: '必填', trigger: 'blur' }]"
          >
            <el-input
              v-model.number="config.channels[i].uv_min"
              placeholder=""
              class="w"
            >
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <div style="padding-left: 10px;padding-top:5px">
              <el-button type="primary" @click="delChannel(i)" size="small"
            >删除渠道
          </el-button>
          </div>
          
        </div>
      </template>

      <div style="text-align: left">
        <el-button
          type="primary"
          @click="updateData()"
          :loading="updateLoading"
        >
          确定修改
        </el-button>
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >1～7点不检测</span
        >
      </div>
    </el-form>
  </div>
</template>
<script>
import {
  get_data_monitoring_config,
  set_data_monitoring_config,
} from "@/api/cnzz/dashboard";
export default {
  data() {
    return {
      config: {},
      updateLoading: false,
      rules: {
        uv_max: [{ required: true, message: "必填", trigger: "blur" }],
        uv_min: [
          { required: true, message: "必填", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value >= this.config.uv_max) {
                callback(new Error("uv点击率最小值不能大于等于最大值！"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        phone: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.get_config();
  },
  methods: {
    get_config() {
      get_data_monitoring_config().then((res) => {
        if (res.STATUS == "200") {
          this.config = res.DATA;
        } else {
          this.$message.error(res.DEC);
        }
      });
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.updateLoading = true;
          set_data_monitoring_config({
            data: JSON.stringify(this.config),
          }).then((res) => {
            if (res.STATUS == "200") {
              this.$message.success("修改成功！");
            } else {
              this.$message.error(res.DEC);
            }
            this.updateLoading = false;
          });
        }
      });
    },
    addChannel() {
      this.config.channels.push({ channel: "", uv_max: 0, uv_min: 0 });
    },
    delChannel(i) {
      this.config.channels.splice(i, 1);
    },
  },
};
</script>
<style>
.w {
  width: 200px;
}
.box {
  padding-bottom: 10px;
  display: flex;
  justify-self: start;
}
</style>